package com.zhejiangzhengyuan.municipal_common.mapper;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhejiangzhengyuan.municipal_common.entity.InspectionRecord;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;

import com.zhejiangzhengyuan.municipal_common.entity.InspectionTasks;
import com.zhejiangzhengyuan.municipal_common.entity.vo.InspectionTasksVo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 巡检记录表，用于记录目前巡检工作产生数据 Mapper 接口
 *
 * @author linju
 * @since 2019-11-22
 */
@Repository
public interface InspectionRecordMapper extends BaseMapper<InspectionRecord> {
  Page<InspectionTasksVo> selectInspectionRecordManageVoPage(
      Page<InspectionTasksVo> page,
      @Param("firstStatus") String firstStatus,
      @Param("secondStatus") String secondStatus);
  // 模糊查询
  Page<InspectionTasksVo> inspectionRecordFuzzy(
      Page<InspectionTasksVo> inspectionTasksVoPage,
      @Param("iLocation") String location,
      @Param("startTimestamp") String startTimestamp,
      @Param("endTimestamp") String endTimestamp);

  @Select(
      "select count(id) from inspection_record where create_time BETWEEN TIMESTAMP '${startTimestamp}' AND TIMESTAMP '${endTimestamp}'")
  int getCountByTime(
      @Param("startTimestamp") String startTimestamp, @Param("endTimestamp") String endTimestamp);

  @Select(
      "select ir.id ,ir.create_time,ir.update_time,ir.target_id,it.*,ui.username \n"
          + "from inspection_record ir,inspection_tasks it,user_info ui\n"
          + "where ir.inspection_tasks_id =it.id and ui.id=ir.inspection_user_info_id")
  List<InspectionTasksVo> selectInspectionRecordManageVo();

  @Select(
      "select ir.id ,ir.create_time,ir.update_time,ir.target_id,it.*,ui.username  \n"
          + "            from inspection_record ir,inspection_tasks it,user_info ui \n"
          + "            where ir.inspection_tasks_id =it.id and ui.id=ir.inspection_user_info_id and ir.id=#{id}")
  InspectionTasksVo selectOneInspectionRecordById(@Param("id") Long id);

  @Select(
      "select it.* from  inspection_record ir,inspection_tasks it where ir.inspection_tasks_id =it.id and ir.id=#{rid}")
  InspectionTasks selectInspectionTasksIdById(@Param("rid") Long id);
}
